<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\AuthGroup as AuthGroupModel;
class AuthGroup extends Common
{
	public function lst()
    {
       $data=db('auth_group')->paginate(4);
       $this->assign('data',$data); 
        return view('list');
    }

    public function add()
    {
        $auth=new AuthGroupModel();
        if(request()->isPost()){
            $data=input('post.');
            if($data['rules']){
                $data['rules']=implode(',',$data['rules']);
            }
                
            //dump($data);die;
            $add=$auth->save($data);
            if($add){
                $this->success('用户组添加成功！',url('auth_group/lst'));
            }else{
                $this->error('用户组添加失败！');
            }
            return;
        }
        $auth= new \app\admin\model\AuthRule;
        $authres=$auth->authTree();
        $this->assign('authres',$authres);
        return view();
    }

    public function edit()
    {
        if(request()->isPost()){
            $input=input('post.');
            //dump($input);die;
            if(!isset($input['status'])){
                $input['status']=0;
            }
            asort($input['rules']);
            $input['rules']=implode(',',$input['rules']);
            
            
            //dump($input);die;
            $save=db('auth_group')->update($input);
            if($save != false){
                $this->success('用户组修改成功！',url('auth_group/lst'));
            }else{
                $this->error('用户组修改失败！');
            }
            return;
        }
        $data=db('auth_group')->find(input('id'));

        $auth= new \app\admin\model\AuthRule;
        $authres=$auth->authTree();
        //dump($data);die;
        $this->assign('authres',$authres);
       $this->assign('data',$data); 
        return view();
    }

    public function delete()
    {
       $del=db('auth_group')->delete(input('id'));
       if($del){
                $this->success('用户组删除成功！',url('auth_group/lst'));
            }else{
                $this->error('用户组删除失败！');
            }
    }

}
